home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
X User Tools
/
X User Tools (O'Reilly and Associates)(1994).ISO
/
sources
/
xpick
/
xpick_11.z
/
xpick_11
/
xpick-1.1
/
xpick.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-21
|
6KB
|
230 lines
/*
* Copyright 1993 The University of Newcastle upon Tyne
*
* Permission to use, copy, modify and distribute this software and its
* documentation for any purpose other than its commercial exploitation
* is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation, and that
* the name of The University of Newcastle upon Tyne not be used in
* advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. The University of
* Newcastle upon Tyne makes no representations about the suitability of
* this software for any purpose. It is provided "as is" without express
* or implied warranty.
*
* THE UNIVERSITY OF NEWCASTLE UPON TYNE DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF
* NEWCASTLE UPON TYNE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* Author: Gerry Tomlinson (gerry.tomlinson@newcastle.ac.uk)
* Computing Laboratory, University of Newcastle upon Tyne, UK
*/
/*****************************************************************************
xpick.c
xpick using MultiList widget
Gerry Tomlinson Feb 1992
****************************************************************************/
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Label.h>
#include "MultiList.h"
#include <stdio.h>
#define MAXITEMS 2000
static char *strings[MAXITEMS];
static char **args;
static Widget top,vport,vpaned,hpaned,ok,set,clear,invert,pattern,multiList;
static void Ok(), ApplyPattern();
main(argc,argv)
int argc;
char **argv;
{
XtAppContext app_con;
int cols = 0;
char buf[BUFSIZ];
int i = 0;
top = XtVaAppInitialize(&app_con,"Xpick",
NULL,0,
&argc,argv,NULL,
NULL);
if (argc == 1) exit(0);
if (argv[1][0] == '-' && argv[1][1]) {
cols = atoi(&argv[1][1]);
if (!cols) {
fprintf(stderr,"xpick: bad number of columns\n");
exit (1);
}
argc--; argv++;
}
if (argc == 1) exit(0);
if (argc == 2 && strcmp(argv[1], "-") == 0) {
while (fgets(buf, sizeof buf, stdin) != NULL) {
buf[strlen(buf)-1] = '\0';
strings[i] = XtNewString(buf);
if (++i == MAXITEMS) {
fprintf(stderr,"xpick: too many input lines\n");
exit(1);
}
}
if (i == 0) exit(0);
strings[i] = 0;
args = strings;
}
else { i = argc ; args = &argv[1];}
vpaned = XtVaCreateManagedWidget("vpaned",panedWidgetClass,top,NULL);
hpaned = XtVaCreateManagedWidget("hpaned",panedWidgetClass,vpaned,
NULL);
ok = XtVaCreateManagedWidget ("ok", commandWidgetClass,hpaned,
NULL);
set = XtVaCreateManagedWidget ("set", commandWidgetClass,hpaned,
NULL);
clear = XtVaCreateManagedWidget ("clear",commandWidgetClass,hpaned,
NULL);
invert = XtVaCreateManagedWidget ("invert",commandWidgetClass,hpaned,
NULL);
pattern = XtVaCreateManagedWidget ("pattern",asciiTextWidgetClass,hpaned,
XtNeditType,XawtextEdit,
NULL);
vport = XtVaCreateManagedWidget ("viewport", viewportWidgetClass,vpaned,
NULL);
if (cols)
multiList = XtVaCreateManagedWidget("multiList",
multiListWidgetClass,vport,
XtNlist,args,
XtNnumberStrings,0,
XtNforceColumns,TRUE,
XtNmaxSelectable,i,
XtNdefaultColumns,cols,
NULL);
else
multiList = XtVaCreateManagedWidget("multiList",
multiListWidgetClass,vport,
XtNlist,args,
XtNnumberStrings,0,
XtNforceColumns,FALSE,
XtNmaxSelectable,i,
NULL);
XtAddCallback(ok,XtNcallback,Ok,(XtPointer)NULL);
XtAddCallback(set,XtNcallback,ApplyPattern,(XtPointer)MultiListHighlightItem);
XtAddCallback(clear,XtNcallback,ApplyPattern,(XtPointer)MultiListUnhighlightItem);
XtAddCallback(invert,XtNcallback,ApplyPattern,(XtPointer)MultiListToggleItem);
XtInstallAccelerators(pattern,set);
XtRealizeWidget(top);
/* reset XtNdefaultColumns to allow recomputation if user resizes */
if (!cols)
XtVaSetValues(multiList,XtNdefaultColumns,0,NULL);
XtSetKeyboardFocus(top,pattern);
XtAppMainLoop(app_con);
} /* End main */
/*ARGSUSED*/
static void
Ok(widget,client_data,call_data)
Widget widget;
XtPointer client_data;
XtPointer call_data;
{
int i;
String *str_ptr;
Boolean *h_ptr,*s_ptr;
MultiListReturnStruct *rs;
rs = MultiListGetHighlighted(multiList);
for (i = 0; i < rs->num_selected; i++)
{
MultiListGetItemInfo(multiList,rs->selected_items[i],&str_ptr,&h_ptr,&s_ptr);
printf("%s\n",str_ptr);
}
exit(0);
}
/*ARGSUSED*/
static void ApplyPattern (w,client_data,call_data)
Widget w;
XtPointer client_data;
XtPointer call_data;
{
typedef void (*multiListProc)();
extern Boolean match();
String p,m;
String empty = "*";
int i;
Boolean gotmatch = FALSE;
XtVaGetValues(pattern, XtNstring, &p, NULL);
if (!p[0])
p = empty;
m = XtNewString(p);
for(i=0; p[i]; i++) {
switch (p[i]) {
case '?':
case '*':
case '[':
m[i] = 1;
break;
default:
m[i] = 0;
}
}
for (i=0; args[i]; i++)
if (match(p, m, args[i])) {
((multiListProc)client_data)(multiList,i);
gotmatch = TRUE;
}
if (!gotmatch) XBell(XtDisplay(top),0);
XtFree(m);
}